package com.ysu.demo.handler;

import com.ysu.demo.VO.ResultVO;
import com.ysu.demo.enums.ExceptionEnum;
import com.ysu.demo.exception.BaseException;
import com.ysu.demo.utlis.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ：lhw
 * @date ：Created in 2019/8/9 10:22
 */
@ControllerAdvice
@ResponseBody
public class ExceptionHandlers {
    @ExceptionHandler
    public ResultVO handler(Exception e){
        Integer code = 500;
        if (e instanceof IllegalArgumentException){
            return ResultUtil.ErrorResult(403,ExceptionEnum.NOT_FIND.getMsg());
        }

        if(e instanceof BaseException){
            code = ((BaseException) e).getCode();
        }

        return ResultUtil.ErrorResult(code,e.getMessage());
    }
}
